<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyWord" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="query">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="menus" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="120" >
      </el-table-column>
      <el-table-column prop="menu.name" label="父级菜单" width="150" >
      </el-table-column>
      <el-table-column prop="url" label="访问URL" width="100">
      </el-table-column>
      <el-table-column prop="component" label="组件路径" width="120" >
      </el-table-column>
      <el-table-column prop="icon" label="图标" min-width="120" >
      </el-table-column>
      <el-table-column prop="intro" label="简介" min-width="100" >
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100">
        <template scope="scope">
          <span v-if="scope.row.state">启用</span>
          <span v-else-if="!scope.row.state">禁用</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
      </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑/新增界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">

      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="上级菜单" prop="parent">
          <el-cascader
              v-model="editForm.parent"
              :options="options"
              :props="{ checkStrictly: true, value:'id',label:'name'}"
              clearable></el-cascader>
        </el-form-item>
        <el-form-item label="名称" prop="editFormRules.name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="index">
          <el-input-number v-model="editForm.index" controls-position="right" @change="handleChange" :min="1"
                           :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="访问URL" prop="editFormRules.sn">
          <el-input v-model="editForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件路径" prop="editFormRules.sn">
          <el-input v-model="editForm.component" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="标识" prop="editFormRules.sn">
          <el-input v-model="editForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="editFormRules.sn">
          <el-input v-model="editForm.icon" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="editForm.state">
            <el-radio class="radio" :label="true">启用</el-radio>
            <el-radio class="radio" :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getMenuListPage, removeMenu, batchRemoveMenu, editMenu, addMenu } from '../../api/api';

export default {
  data() {
    return {
      options:[],
      filters: {
        keyWord: ''
      },
      menus: [],
      total: 0,
      pageSize:5,
      currentPage:1,
      listLoading: false,
      sels: [],//列表选中列
      title:"",
      editFormVisible: false,//编辑界面是否显示

      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入部门编号', trigger: 'blur' }
        ],
        manager: [
          { required: false, message: '请输入负责人', trigger: 'blur' }
        ],
        parent: [
          { required: false, message: '请输入上级部门', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      editForm: {
        id:null,
        sn:"",
        name:"",
        state:0,
        manager:{},
        parent:{}
      },
      emp:[]
    }
  },
  methods: {
    getparent(){
      this.$http.get("/menu/tree")
          .then(result=>{
            this.options = result.data;

          })
    },
    getemp(){
      this.$http.get("/employee")
          .then(result=>{
            this.emp = result.data;
            this.getMenus();
          })
          .catch(result=>{
            this.$message({
              message: result.message,
              type: 'error'
            });
          })
    },

    handleCurrentChange(val) {
      this.currentPage = val;
      this.getMenus();
    },
    query(){
      this.currentPage = 1;
      this.getMenus();
    },
    //获取部门列表
    getMenus() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyWord: this.filters.keyWord
      };
      console.log(this.filters.keyWord)
      this.listLoading = true;
      this.$http.post("/menu/list",para)
          .then(result=>{
            console.log(result.data.rows);
            this.listLoading = false;
            this.menus = result.data.rows;
            this.total = result.data.total;
          })
          .catch(result=>{
            this.$message({
              message: '系统错误',
              type: 'error'
            });
          })
    },
    //删除
    handleDel: function (index, row) {
      this.currentPage = 1;
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.delete("/menu/" + row.id)
            .then(result=>{
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '操作成功',
                  type: 'error'
                });
              }
              // 调用查询方法
              this.getMenus();
            })

      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title="编辑";
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
      // let npa = row.printId
      // let parent = [];
      // for (let i = 1; i < npa.length-1; i++) {
      //   parent.push(parseInt(npa[i]));
      // }
      var parentId = row.parentId;
      var id = row.id;
      if (parentId){
        this.editForm.parent = [parentId,id];
      }else{
        this.editForm.parent = [id];
      }


    },
    //显示新增界面
    handleAdd: function () {
      this.title="新增";
      this.editFormVisible = true;
      this.editForm = {
        id:null,
        sn:"",
        name:"",
        state:0,
        manager:{},
        parent:{}
      };
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editForm);
            this.editFormVisible = false;
            para.menu = {'id':para.parent[para.parent.length - 1]}
            this.$http.put("/menu",para)
                .then(result=>{
                  this.editLoading = false;
                  if (result.data.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '操作成功',
                      type: 'error'
                    });
                  }
                  // 调用查询方法
                  this.getMenus();
                  // 调用查询上级部门
                  this.getparent();
                })

          }).catch(() => {
            this.editFormVisible = false;
          });
        }
      });
    },
    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.addForm);
            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            addMenu(para).then((res) => {
              this.addLoading = false;
              //NProgress.done();
              this.getparent();
              this.$message({
                message: '提交成功',
                type: 'success',
              });
              this.$refs['addForm'].resetFields();
              this.addFormVisible = false;
              this.getMenus();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      this.currentPage = 1;
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.patch("/menu",ids)
            .then(result=>{
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '操作异常',
                  type: 'error'
                });
              }
              // 调用查询方法
              this.getMenus();
            })
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getMenus();
    this.getemp();
    this.getparent();
  }
}

</script>

<style scoped>

</style>